/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.clinicplus.manager.search;

import br.com.foxline.clinicplus.entity.Cash;
import br.com.foxline.clinicplus.entity.Income;
import br.com.foxline.clinicplus.enums.PaymentType;
import br.com.foxline.clinicplus.enums.TypeDebt;
import br.com.foxline.clinicplus.enums.TypeIncome;
import br.com.foxline.clinicplus.service.CashService;
import br.com.foxline.clinicplus.service.DoctorService;
import br.com.foxline.clinicplus.service.IncomeService;
import br.com.foxline.clinicplus.service.ProcedureService;
import br.com.foxline.clinicplus.service.SystemUserService;
import br.com.foxline.clinicplus.util.Msg;
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;

/**
 *
 * @author roniere
 */
@ManagedBean
@ViewScoped
public class ManagerSearchFinancialOutput implements Serializable {

    @EJB
    IncomeService incomeService;
    @EJB
    DoctorService doctorService;
    @EJB
    ProcedureService procedureService;
    @EJB
    SystemUserService systemUserService;
    @EJB
    CashService cashService;
    private Income income;
    private Income incomeeditar;
    private Income incomeremover;
    private List<Income> incomes;
    private BigDecimal total;
    
    private Date dateInitial;
    private Date dateFinal;

    @PostConstruct
    public void init() {
        income = new Income();
        total = new BigDecimal(BigInteger.ZERO);
        incomeeditar = new Income();
    }

    public List<SelectItem> selectTypeOutput() {
        List<SelectItem> list = new ArrayList<SelectItem>();

        for (TypeDebt ms : TypeDebt.values()) {
            list.add(new SelectItem(ms, ms.getDesc()));
        }
        return list;
    }

    public Date getDateFinal() {
        return dateFinal;
    }

    public void setDateFinal(Date dateFinal) {
        this.dateFinal = dateFinal;
    }

    public Date getDateInitial() {
        return dateInitial;
    }

    public void setDateInitial(Date dateInitial) {
        this.dateInitial = dateInitial;
    }

    
    public Income getIncome() {
        return income;
    }

    public void setIncome(Income income) {
        this.income = income;
    }

    public List<Income> getIncomes() {
        return incomes;
    }

    public void setIncomes(List<Income> incomes) {
        this.incomes = incomes;
    }

    public BigDecimal getTotal() {
        return total;
    }

    public void setTotal(BigDecimal total) {
        this.total = total;
    }

    public Income getIncomeeditar() {
        return incomeeditar;
    }

    public void setIncomeeditar(Income incomeeditar) {
        this.incomeeditar = incomeeditar;
    }

    public void search() {
        System.err.println(dateInitial);
        System.err.println(dateFinal);
        System.err.println(income.getTypeDebt());
        total = incomeService.getValueIncomeGeneric(null, null, null, dateInitial, dateFinal, null, null, TypeIncome.D, income.getTypeDebt(), null, null, null, null, null, null, null, null);
        incomes = incomeService.findIncomeGeneric(null, null, null, dateInitial, dateFinal, null, null, TypeIncome.D, income.getTypeDebt(), null, null, null, null, null, null, null, null);
    }

    public void loadEditar(int id) {
        incomeeditar = incomes.get(id);
    }
    public void loadRemover(int id) {
        incomeremover = incomes.get(id);
    }

    public void editar() {
        try {
            if (incomeeditar.getId() != null) {
                incomeeditar.setAmountPaid(incomeeditar.getTotalAmount());
                incomeService.update(incomeeditar);
                Msg.messagemInfo("Registro atualizado com Sucesso !");
            }
            incomeeditar = new Income();
            search();
            
        } catch (Exception e) {
            Msg.messagemError("Erro na operação !");
           e.printStackTrace();
        }
    }
    
    public void remover() {
        try {
            if (incomeremover.getId() != null) {
                incomeremover.setAtivo(false);
                //REMOVENDO CASH
                if(incomeremover.getCash() != null){
                    Cash c = incomeremover.getCash();
                    c.setStatus(false);
                    cashService.update(c);
                }
                incomeService.update(incomeremover);
                Msg.messagemInfo("Registro excluído com Sucesso !");
            }
            incomeremover = new Income();
            search();
        } catch (Exception e) {
            Msg.messagemError("Erro na operação !");
           e.printStackTrace();
        }
    }
}
